﻿using System;
using Unity.Type.BinaryType;
using Xunit;

namespace Unity.Type.Tests
{
    public class MetadataESerializerTest
    {
        private static readonly Random Rand = new Random();
        private static string CreateRandom()
        {
            int randomLength = 19;
            const string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            char[] symbols = new char[randomLength];
            for (int index = 0; index < randomLength; index++)
            {
                symbols[index] = allowedChars[Rand.Next(0, allowedChars.Length)];
            }

            return new string(symbols);
        }

        private static string CreateCounter()
        {
            int counterLength = 5;
            const string allowedChars = "0123456789";
            char[] symbols = new char[counterLength];
            for (int index = 0; index < counterLength; index++)
            {
                symbols[index] = allowedChars[Rand.Next(0, allowedChars.Length)];
            }

            return new string(symbols);
        }

        private const string TestCsiId = "144347";
        private const int TestChangerCsiId = 144347;
        private static readonly byte TestDelimiter = Convert.ToByte(':');
        private static readonly byte TestSpecVersion = Convert.ToByte('1');
        private static readonly string TestRandom = CreateRandom();
        private static readonly string TestCounter = CreateCounter();
        private static readonly long TestVersion = DateTimeOffset.Now.ToUnixTimeSeconds();
        private static readonly long TestVersionDelete = DateTimeOffset.Now.ToUnixTimeSeconds();
        private static readonly EntityType TestEntityType = EntityType.Order;
        private static readonly EventType TestEventType = EventType.Create;
        private const int TestFormatVer = 1;

        public static MetadataE PrepareMetadataObject()
        {
            MetadataE metadata = new MetadataE();
            metadata.SetCsiId(TestCsiId);
            metadata.Delimiter = TestDelimiter;
            metadata.SpecVersion = TestSpecVersion;
            metadata.SetRandom(TestRandom);
            metadata.SetCounter(TestCounter);
            metadata.Version = TestVersion;
            metadata.ChangerCsiId = TestChangerCsiId;
            metadata.Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            metadata.EntityType = TestEntityType;
            metadata.EventType = TestEventType;
            metadata.FormatVer = TestFormatVer;
            metadata.VersionDelete = TestVersionDelete;
            return metadata;
        }

        public static byte[] PrepareMetadataSerializedData(MetadataE metadataObj)
        {
            MetadataESerializer serializer = new MetadataESerializer();
            return serializer.Serialize(metadataObj);
        }

        [Fact]
        public void MethodsTest()
        {
            MetadataE testMetadata = PrepareMetadataObject();
            byte[] serialMetadata = PrepareMetadataSerializedData(testMetadata);
            Assert.True(serialMetadata.Length > 0);
        }

    }
}